<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Taichinh_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	}
	
	function odaStore()
	{
		
	}
	
	function do_exp_excel($duration)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->do_exp_excel_thang(); break;
			case 'LASTMONTH':
					return $this->do_exp_excel_thang_truoc(); break;
			case 'THISYEAR':
				return $this->do_exp_excel_nam(); break;
			case 'QUATER':
				return $this->do_exp_excel_quy(); break;
			case 'HALF':
				return $this->do_exp_excel_nua(); break;
			default :
				return $this->do_exp_excel_thang(); break;
		}
	}
	
	function do_exp_excel_thang()
	{
		$return = array();
		//1. Get data
		$tong_hop = $this->get_report_tong_hop_thang();
		$nguon = $this->get_report_tong_hop_nguon_thang();
		
		$return['tong_hop'] = $tong_hop;
		$return['nguon'] = $nguon;
		return $return;
	}
	
	function do_exp_excel_thang_truoc()
	{
		$return = array();
		$tong_hop = $this->get_report_tong_hop_thang_truoc();
		$nguon = $this->get_report_tong_hop_nguon_thang_truoc();
		$return['tong_hop'] = $tong_hop;
		$return['nguon'] = $nguon;
		return $return;
	}
	
	function do_exp_excel_nam()
	{
		
	}
	
	function do_exp_excel_quy()
	{
		
	}
	
	function do_exp_excel_nua()
	{
		
	}
	
	function do_exp_excel_thang1()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->order_by('f_tong_hop.id','desc');
		$this->db->where('f_tong_hop.kind', 0);
		$this->db->where('f_hang_muc.kind', 1);
		$this->db->where('YEAR(f_tong_hop.created_date)',date('Y'));
		$this->db->where('MONTH(f_tong_hop.created_date)',date('m'));
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_thang($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	///////////// END EXPORT ///////////
	
	function tao_bc_tong_hop()
	{
		//1. Lấy danh mục đã submit
		$danhmucs = $this->get_all_category(1,1);
		$bc_tong_hop = $this->get_report_tong_hop_thang();
		if($bc_tong_hop)
		{
			return false;
		}
		$pre_bc_tong_hop = $this->get_report_tong_hop_thang_truoc();
		//var_dump($pre_bc_tong_hop);
		if($pre_bc_tong_hop == null)
		{
			//2. Lấy danh sách đầu kỳ khởi tạo
			$khoi_tao = $this->get_all_sddk();
			//var_dump($khoi_tao); exit();
			foreach($danhmucs as $danhmuc)
			{
				foreach($khoi_tao as $j)
				{
					if($j['hang_muc_id']==$danhmuc['id'])
					{
						$this->add_f_tong_hop($danhmuc['id'],$j['sddk']);
					}
				}
			}
		}
		else
		{
			$newrows = array();
			$listdm = array();
			$listpbc = array();
			foreach($danhmucs as $danhmuc)
			{
				$flag = 0;
				foreach($pre_bc_tong_hop as $j)
				{
					if($j['hang_muc_id']==$danhmuc['id'])
					{
						//a. cập nhật số dư cuối kỳ = sddk+ps tang - ps giảm
						$sum = $this->tinh_tong_ps_theo_hang_muc_thang_truoc($danhmuc['id']);
						if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
						if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;

						$j['sdck'] = $j['sddk']+$sum[0]['tang']- $sum[0]['giam'];
						// cập nhật ps tăng, ps giảm, sdck vào tong hop $j
						$this->update_f_tong_hop($j['id'],$sum[0]['tang'],$sum[0]['giam'],$j['sdck']);
						$this->add_f_tong_hop($danhmuc['id'],$j['sdck']);
					}
					
				}
				$listdm[$danhmuc['id']] = $danhmuc;
			}
			foreach($pre_bc_tong_hop as $j)
			{
				$listpbc[$j['hang_muc_id']] = $j;
			}
			$newrows = array_diff_key($listdm, $listpbc);
			// Nếu có danh mục mới thêm vào, cần lấy giá trị khởi tạo
			if($newrows)
			{
				foreach($newrows as $danhmuc)
				{
					foreach($khoi_tao as $j)
					{
						if($j['hang_muc_id']==$danhmuc['id'])
						{
							if($j['status'] == 1)
							{
								$this->add_f_tong_hop($danhmuc['id'],$j['sddk']);
							}
							else {
								$this->add_f_tong_hop($danhmuc['id'],0);
							}
						}
					}
				}
			}
			
		}
		return true;
	}
	
	function add_f_tong_hop($hangmucid,$sddk)
	{
	
		$fields = array(
				'hang_muc_id'			=> $hangmucid,
				'ps_tang'	=>0,
				'sddk'	=>$sddk,
				'ps_giam'		=> 0,
				'status' =>1,
				'kind' =>0,
		);
		$this->db->set($fields);
		return $this->db->insert('f_tong_hop');
	
	}
	//////////////////////// END RP TONG HOP ///////////////
	function get_report_tong_hop($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_tong_hop_thang(); break;
			case 'LASTMONTH':
				return $this->get_report_tong_hop_thang_truoc(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua(); break;
			default :
				//echo $duration; exit();
				$tmp = explode("_",$duration);
				return $this->get_report_tong_hop_c($tmp[1]); break;
			
		}
		
	}
	// get report custom duration 
	
	function get_report_tong_hop_c($bdate)
	{
		//echo $bdate; exit();
		$bdatet = explode('/', $bdate);
		$m = $bdatet[0];
		$y = $bdatet[1];
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->order_by('f_tong_hop.id','desc');
		$this->db->where('f_tong_hop.kind', 0);
		$this->db->where('f_hang_muc.kind', 1);		
		$this->db->where('YEAR(f_tong_hop.created_date)',$y);
		$this->db->where('MONTH(f_tong_hop.created_date)',$m);
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_c($rs['hang_muc_id'],$bdate);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	
	function tinh_tong_ps_theo_hang_muc_c($hangmucid,$bdate)
	{
		$bdatet = explode('/', $bdate);
		$m = $bdatet[0];
		$y = $bdatet[1];
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('f_chung_tu.hang_muc_id', $hangmucid);
		$this->db->where('MONTH(f_chung_tu.created_date)',$m);
		$this->db->where('YEAR(f_chung_tu.created_date)',$y);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_report_tong_hop_thang_truoc()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->order_by('f_tong_hop.id','desc');
		$this->db->where('f_tong_hop.kind', 0);
		$this->db->where('f_hang_muc.kind', 1);
		if(date('m') == 1)
		{
			$this->db->where('YEAR(f_tong_hop.created_date)',date('Y')-1);
			$this->db->where('MONTH(f_tong_hop.created_date)',12);
		}
		else 
		{
			$this->db->where('YEAR(f_tong_hop.created_date)',date('Y'));
			$this->db->where('MONTH(f_tong_hop.created_date)',date('m')-1);
		}
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_thang_truoc($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	
	function get_report_tong_hop_thang()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->order_by('f_tong_hop.id','desc');
		$this->db->where('f_tong_hop.kind', 0);
		$this->db->where('f_hang_muc.kind', 1);
		$this->db->where('YEAR(f_tong_hop.created_date)',date('Y'));
		$this->db->where('MONTH(f_tong_hop.created_date)',date('m'));
		$query = $this->db->get();		
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_thang($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	function get_report_tong_hop_nam()
	{
		return null;
	}
	function get_report_tong_hop_quy()
	{
		return null;
	}
	function get_report_tong_hop_nua()
	{
		return null;
	}
	// Report tong hop theo nguon tien {ngan sach, hoc phi, cac hang muc chi}
	function get_report_tong_hop_nguon($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_tong_hop_nguon_thang(); break;
			case 'LASTMONTH':
					return $this->get_report_tong_hop_nguon_thang_truoc(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nguon_nam(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_nguon_quy(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nguon_nua(); break;
			default :
				return $this->get_report_tong_hop_nguon_thang(); break;
		}
		
	}
	
	function get_report_tong_hop_nguon_thang_truoc()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->order_by('f_tong_hop.id','desc');
		$this->db->where('f_tong_hop.kind', 0);
		$this->db->where('f_hang_muc.kind', 0);// ngoai he thong
		if(date('m') == 1)
		{
			$this->db->where('YEAR(f_tong_hop.created_date)',date('Y')-1);
			$this->db->where('MONTH(f_tong_hop.created_date)',12);
		}
		else {
			$this->db->where('YEAR(f_tong_hop.created_date)',date('Y'));
			$this->db->where('MONTH(f_tong_hop.created_date)',date('m') - 1);
		}
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_nguon_thang_truoc($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	
	function get_report_tong_hop_nguon_thang()
	{
		$this->db->select('f_hang_muc.*');
		$this->db->from('f_hang_muc');
		$this->db->where('f_hang_muc.kind', 0);// ngoai he thong
		
		$query = $this->db->get();
		
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_nguon_thang($rs['id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	
	function tinh_tong_ps_theo_hang_muc_nguon($hangmucid,$duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->tinh_tong_ps_theo_hang_muc_nguon_thang($hangmucid); break;
			case 'THISYEAR':
				return $this->tinh_tong_ps_theo_hang_muc_nguon_nam($hangmucid); break;
			case 'QUATER':
				return $this->tinh_tong_ps_theo_hang_muc_nguon_quy($hangmucid); break;
			case 'HALF':
				return $this->tinh_tong_ps_theo_hang_muc_nguon_nua($hangmucid); break;
			default :
				return $this->tinh_tong_ps_theo_hang_muc_nguon_thang($hangmucid); break;
		}
	}
	
	function tinh_tong_ps_theo_hang_muc_nguon_thang_truoc($hangmucid)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('f_chung_tu.nguon_id', $hangmucid);
		$this->db->where('YEAR(f_chung_tu.created_date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH');
		$this->db->where('MONTH(f_chung_tu.created_date = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function tinh_tong_ps_theo_hang_muc_nguon_thang($hangmucid)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('f_chung_tu.nguon_id', $hangmucid);
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		return $query->result_array();
	}
	// End Report tong hop theo nguon tien {ngan sach, hoc phi, cac hang muc chi}
	
	// TÍnh tông PS trong kỳ đối với mỗi hạng mục
	function tinh_tong_ps_theo_hang_muc($hangmucid,$duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->tinh_tong_ps_theo_hang_muc_thang($hangmucid); break;
			case 'THISYEAR':
				return $this->tinh_tong_ps_theo_hang_muc_nam($hangmucid); break;
			case 'QUATER':
				return $this->tinh_tong_ps_theo_hang_muc_quy($hangmucid); break;
			case 'HALF':
				return $this->tinh_tong_ps_theo_hang_muc_nua($hangmucid); break;
			default :
				return $this->tinh_tong_ps_theo_hang_muc_thang($hangmucid); break;
		}
	
	}
	
	function tinh_tong_ps_theo_hang_muc_thang_truoc($hangmucid)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('f_chung_tu.hang_muc_id', $hangmucid);
		
		if(date('m') -1 == 0 )
		{
			$this->db->where('YEAR(f_chung_tu.created_date)',date('Y')-1);
			$this->db->where('MONTH(f_chung_tu.created_date)',date('m'));
		}
		else
		{
			$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
			$this->db->where('MONTH(f_chung_tu.created_date)',date('m')-1);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function tinh_tong_ps_theo_hang_muc_thang($hangmucid)
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('f_chung_tu.hang_muc_id', $hangmucid);
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		return $query->result_array();
	}
	function tinh_tong_ps_theo_hang_muc_nam($hangmucid)
	{
		return false;
	}
	function tinh_tong_ps_theo_hang_muc_quy($hangmucid)
	{
		return false;
	}
	function tinh_tong_ps_theo_hang_muc_nua($hangmucid)
	{
		return false;
	}
	
	// Tính tổn PS trong kỳ
	function tinh_tong_ps($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->tinh_tong_ps_theo_thang(); break;
			case 'TODAY':
					return $this->tinh_tong_ps_theo_today(); break;
			case 'LASTMONTH':
					return $this->tinh_tong_ps_theo_thang_truoc(); break;
			case 'THISYEAR':
				return $this->tinh_tong_ps_theo_nam(); break;
			case 'QUATER':
				return $this->tinh_tong_ps_theo_quy(); break;
			case 'HALF':
				return $this->tinh_tong_ps_theo_nua(); break;
			case 'YESTERDAY':
				return $this->tinh_tong_ps_theo_homqua(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->tinh_tong_ps_theo_c($tmp[1],$tmp[2]); break;
		}
		
	}
	
	function tinh_tong_ps_theo_c($bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('f_chung_tu.created_date >= ',$ngaybd);
		$this->db->where('f_chung_tu.created_date <= ',$ngaykt);
		$query = $this->db->get();
		
		return $query->result_array();
	}
	function tinh_tong_ps_theo_today()
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m'));
		$this->db->where('DAY(f_chung_tu.created_date)',date('d'));
		$query = $this->db->get();
		
		return $query->result_array();
	}
	function tinh_tong_ps_theo_thang_truoc()
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		if(date('m') == 1)
		{
			$this->db->where('YEAR(f_chung_tu.created_date)',date('Y') - 1);
			$this->db->where('MONTH(f_chung_tu.created_date)',12);
		}
		else {
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m') -1);
		}
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	function tinh_tong_ps_theo_thang()
	{
		$this->db->select('SUM(ps_tang) as tang, SUM(ps_giam) as giam');
		$this->db->from('f_chung_tu');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		
		return $query->result_array();
	}
	
	function tinh_tong_ps_theo_nam()
	{
		return null;
	}
	function tinh_tong_ps_theo_quy()
	{
		return null;
	}
	function tinh_tong_ps_theo_nua()
	{
		return null;
	}
	function tinh_tong_ps_theo_homqua()
	{
		return null;
	}
	function update_f_tong_hop($id,$ps_tang,$ps_giam,$ck)
	{

		$fields = array(
				'ps_tang'			=> $ps_tang,
				'ps_giam'			=> $ps_giam,
				'sdck'=>$ck
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('f_tong_hop');
	}
	function submit_chung_tu($id)
	{

		$fields = array(
				'status'			=> 1,
				'created_date' => $datetime = date('Y-m-d H:i:s')
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('f_chung_tu');
	}
	function save_chung_tu($id,$ps_tang,$ps_giam)
	{
		$ps_tang = str_replace(',','',$ps_tang); //clean ,
		$ps_giam = str_replace(',','',$ps_giam);
		$fields = array(
				'ps_tang'			=> $ps_tang,
				'ps_giam'			=> $ps_giam
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->where('status', 0);
		return $this->db->update('f_chung_tu');
	}
	
	function themchungtu($hang_muc_id,$ps_tang,$ps_giam,$note,$nguon_id)
	{
		$ps_tang = str_replace(',','',$ps_tang); //clean ,
		$ps_giam = str_replace(',','',$ps_giam);
		$fields = array(
				'hang_muc_id'			=> $hang_muc_id,
				'ps_tang'	=>$ps_tang,
				'ps_giam'		=> $ps_giam,
				'note'=>$note,
				'nguon_id'=>$nguon_id,
				'status' =>0
		);
		
		$this->db->set($fields);
		$this->db->insert('f_chung_tu');
		return $this->db->insert_id();
	}
	
	function get_all_chung_tu_ok($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_all_chung_tu_ok_theo_thang(); break;
			case 'TODAY':
					return $this->get_all_chung_tu_ok_theo_today(); break;
			case 'LASTMONTH':
					return $this->get_all_chung_tu_ok_theo_thang_truoc(); break;
			case 'THISYEAR':
				return $this->get_all_chung_tu_ok_theo_nam(); break;
			case 'QUATER':
				return $this->get_all_chung_tu_ok_theo_quy(); break;
			case 'HALF':
				return $this->get_all_chung_tu_ok_theo_nua(); break;
			case 'YESTERDAY':
				return $this->get_all_chung_tu_ok_theo_homqua(); break;
			default :
				$tmp = explode("_",$duration);
				return $this->get_all_chung_tu_ok_theo_c($tmp[1],$tmp[2]); break;
		}
	
	}
	
	function get_all_chung_tu_ok_theo_c($bdate,$edate)
	{
		$bdatet = str_replace('/', '-', $bdate);
		$edatet = str_replace('/', '-', $edate);
		$ngaybd = date('Y-m-d', strtotime($bdatet));
		$ngaykt = date('Y-m-d', strtotime($edatet));
		
		$this->db->select('f_chung_tu.*, f_hang_muc.name as item');
		$this->db->from('f_chung_tu');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_chung_tu.hang_muc_id', 'left');
		$this->db->order_by('f_chung_tu.id','desc');
		$this->db->where('f_chung_tu.status', 1);
		
		$this->db->where('f_chung_tu.created_date >=',$ngaybd);
		$this->db->where('f_chung_tu.created_date <=',$ngaykt);
		
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_all_chung_tu_ok_theo_thang_truoc()
	{
		$this->db->select('f_chung_tu.*, f_hang_muc.name as item');
		$this->db->from('f_chung_tu');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_chung_tu.hang_muc_id', 'left');
		$this->db->order_by('f_chung_tu.id','desc');
		$this->db->where('f_chung_tu.status', 1);
		if(date('m') == 1)
		{
			$this->db->where('YEAR(f_chung_tu.created_date)',date('Y') - 1);
			$this->db->where('MONTH(f_chung_tu.created_date)',12);
		}
		else {
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m')-1);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
	function get_all_chung_tu_ok_theo_today()
	{
		$this->db->select('f_chung_tu.*, f_hang_muc.name as item');
		$this->db->from('f_chung_tu');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_chung_tu.hang_muc_id', 'left');
		$this->db->order_by('f_chung_tu.id','desc');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m'));
		$this->db->where('DAY(f_chung_tu.created_date)',date('d'));
		$query = $this->db->get();
		return $query->result_array();
	}
	function get_all_chung_tu_ok_theo_thang()
	{
		$this->db->select('f_chung_tu.*, f_hang_muc.name as item');
		$this->db->from('f_chung_tu');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_chung_tu.hang_muc_id', 'left');
		$this->db->order_by('f_chung_tu.id','desc');
		$this->db->where('f_chung_tu.status', 1);
		$this->db->where('YEAR(f_chung_tu.created_date)',date('Y'));
		$this->db->where('MONTH(f_chung_tu.created_date)',date('m'));
		$query = $this->db->get();
		
		return $query->result_array();
	}
	function get_all_chung_tu_ok_theo_nam()
	{
		return null;
	}
	function get_all_chung_tu_ok_theo_quy()
	{
		return null;
	}
	function get_all_chung_tu_ok_theo_nua()
	{
		return null;
	}
	function get_all_chung_tu_ok_theo_homqua()
	{
		return null;
	}
	function get_all_chung_tu()
	{
		$this->db->select('f_chung_tu.*, f_hang_muc.name as item, nguon.name as nguon');
		$this->db->from('f_chung_tu');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_chung_tu.hang_muc_id', 'left');
		$this->db->join('f_hang_muc as nguon', 'nguon.id = f_chung_tu.nguon_id', 'left');
		$this->db->order_by('f_chung_tu.id','desc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// Edit SDDK
	
	function submitsddk($id)
	{
		$fields = array(
				'status'			=> 1
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('f_tong_hop');
	}
	function saveiteminstore($id,$amount)
	{
		$fields = array(
				'sddk'			=> $amount
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('f_tong_hop');
	}
	
	// Nhập số dư đầu kỳ cho các hạng mục
	function nhap_do_du_dau_ky( $hangmucid,$sddk)
	{
		$fields = array(
				'hang_muc_id'			=> $hangmucid,
				'ps_tang'	=>0,
				'ps_giam'		=> 0,
				'sddk' =>$sddk,
				'status' =>0,
				'kind' =>1
		);
		
		$this->db->set($fields);
		return $this->db->insert('f_tong_hop');
	}
	
	// Lây danh sách do du dau ky nhap
	function get_all_sddk()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->where('f_tong_hop.kind', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	//tự động chạy khi ngày đầu tiên của tháng tiếp theo
	function create_tong_hop()
	{
		//1. Lấy ra danh sách hạng mục
		
		$categories = $this->get_all_category(1);
		
	}
	
	function report_tong_hop()
	{
		
	}
	
	// Phát sinh trong kỳ
	function report_chi_tiet()
	{
		
	}
	
	function nhap_phieu_chi($hangmucid,$psgiam)
	{
		return $this->add_chung_tu($hangmucid, 0, $psgiam);
	}
	function nhap_phieu_thu($hangmucid,$pstang)
	{
		return $this->add_chung_tu($hangmucid, $pstang, 0);
	}
	// Insert bang chung tu
	function add_chung_tu($hangmucid,$pstang,$psgiam)
	{
		$fields = array(
				'hang_muc_id'			=> $hangmucid,
				'ps_tang'	=>$pstang,
				'ps_giam'		=> $psgiam,
				'status' =>0
		);
	
		$this->db->set($fields);
		return $this->db->insert('f_chung_tu');
	}
	
	
	// START f_hang_muc
	
	
	// count all category
	function get_all_category($status = '',$kind=0)
	{
		$this->db->select('*');
		$this->db->from('f_hang_muc');
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		if($kind != 3)
		{
			$this->db->where('kind', $kind);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// add category
	function add_category()
	{
		$fields = array(
				'name'			=> $this->input->post('name'),
				'description'	=> $this->input->post('description'),
				'status'		=> 0,
		);
	
		$this->db->set($fields);
		$this->db->insert('f_hang_muc');
	}
	
	// Edit category
	public function edit_category($id)
	{
		$fields = array(
				'name'			=> $this->input->post('name'),
				'description'			=> mb_strtoupper($this->input->post('description'))
		);
	
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('f_hang_muc');
	}
	
	// get category
	function get_category($id)
	{
		$this->db->select('*');
		$this->db->from('f_hang_muc');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function enablecate($id)
	{
		$fields = array(
				'status'		=> 1
		
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('f_hang_muc');
	}
	
	function get_all_hangmuc($limit, $start)
	{
		$this->db->select('*');
		$this->db->from('f_hang_muc');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_hangmuc($id)
	{
		$this->db->select('*');
		$this->db->from('f_hang_muc');
		$query = $this->db->get();
		return $query->row();
	}
	
	public function count_all_hangmuc()
	{
		$this->db->select('id');
		$this->db->from('f_hang_muc');
		return $this->db->count_all_results();
	}
	
	function add_hangmuc()
	{
		$fields = array(
				'name' => $this->input->post('name'),
				'description'=> $this->input->post('description'),
				'status'=> $this->input->post('status'),
		);
		$this->db->set($fields);
		$this->db->insert('f_hang_muc');
	}
	
	function edit_hangmuc($id)
	{
		$field = array(
				'name' => $this->input->post('name'),
				'description'=> $this->input->post('description'),
				'status'=> $this->input->post('status'),
		);
		$this->db->set($field);
		$this->db->where('id',$id);
		$this->db->update('f_hang_muc');
	}
	
	public function change_status_hangmuc($id = null,$status = 0)
	{
		$data = array
		(
				'status'=> $status
		);
		$this->db->where('id',$id);
		$this->db->update('f_hang_muc',$data);
	}
	
	// submit danh muc
	public function submit_hang_muc($id=null)
	{
		$data = array('status' => 1);
		$this->db->where('id', $id);
		$this->db->set($data);
		$this->db->update('f_hang_muc');
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	// END CATEGORY CONTROLLER
}

/* Location: ./application/models/bangdiem_model.php */